<?php
class ControllerFriend { 

    function getFunction($op){
        
        switch ($op) {
            case "new":
                $this->newFriend();
                break;
            
            case "list":
                $this->listFriend();
                break;
            
            case "save":
                $this->saveFriend();
                break;

            case "edit":
                $this->editFriend();
                break;
            
            case "update":
                $this->updateFriend();
                break;
            
            case "del":
                $this->deleteFriend();
                break;
            
            case "combo":
                return $this->comboFriend();
                break;
            
            default:
                echo "ERROR NO HAY CONTROLADOR PARA ESA OPCION";
                break;
        }
    }
    

    function newFriend(){
        include_once "vista/vista.friend.php";
        form_friend();
    }    
    

    function saveFriend(){
        include_once "modelo/modelo.friend.php";
        
        $f = new friend();
        $f->set_f_u_id($_REQUEST["f_u_id"]); 
        $f->set_f_list($_REQUEST["f_list"]); 

        
        if($f->Save()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_save_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_save_error']." </div>";
        }
        $this->listFriend($msj);
        
    }    
    
    function editFriend(){
        include_once 'modelo/modelo.friend.php';
        include_once 'vista/vista.friend.php';
        
        $f = new friend();
        $f->setf_u_id($_REQUEST["f_u_id"]); 
        
        $f->getData();
        
        form_friend($f);
        
    }    
    
    function deleteFriend(){
        include_once 'modelo/modelo.friend.php';
        
        $f = new friend();
        $f->setf_u_id($_REQUEST["f_u_id"]); 
        
        if($f->Delete()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_delete_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_delete_error']." </div>";
        }
        $this->listFriend($msj);
    }

    
    function updateFriend(){
        include_once 'modelo/modelo.friend.php';
        
        $f = new friend();
        $f->set_f_u_id($_REQUEST["f_u_id"]); 
        $f->set_f_list($_REQUEST["f_list"]); 

        if($f->Update()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_update_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_update_error']." </div>";
        }
        $this->listFriend($msj);
    }

    
    function listFriend($msj=false){
        include_once 'modelo/modelo.friend.php';
        include_once 'vista/vista.friend.php';
        
        $f = new friend();

        $f->setf_u_id($_REQUEST["f_u_id"]); 
        $f->getData();
        
        $dataTotal = $f->GetList(" count(*) ", false,  true);
        $dataTotal = $dataTotal[0][0]; 
        $data = $f->GetList(" * ", false);
        if(is_null($data)){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_empty_error']." </div>";
        }
        list_friend($data, $msj, $dataTotal);

    }

}